import torch
import os
import sys
sys.path.append('../har_recognition')
from src.models.transformer_model import HARTransformer
from config import MODEL_PATH, INPUT_SIZE, NUM_CLASSES

def load_model():
    """加载预训练的HAR模型"""
    device = 'cuda' if torch.cuda.is_available() else 'cpu'
    model = HARTransformer(input_size=INPUT_SIZE, num_classes=NUM_CLASSES)

    if os.path.exists(MODEL_PATH):
        state_dict = torch.load(MODEL_PATH, map_location=device)
        model.load_state_dict(state_dict)
        print(f"模型已从 {MODEL_PATH} 加载")
    else:
        print(f"警告: 模型文件 {MODEL_PATH} 不存在!")

    model.to(device)
    model.eval()
    return model, device